package cn.ting.mybatis.executor;

import cn.ting.mybatis.po.Configuration;
import cn.ting.mybatis.po.Mapper;
import cn.ting.mybatis.proxy.Converser;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.List;
import java.util.Map;

/**
 * @Author: ting
 */
public class MyExecutor implements Executor{



    private Configuration configuration;


    public MyExecutor(Configuration configuration) {
        this.configuration = configuration;//把opensession中传的myConfiguration传给当前类的成员变量

    }


    @Override
    public <T> List<T> selectList(String Key){
        Connection connection = null;
        PreparedStatement pstm = null;
        ResultSet resultSet = null;
        try {
            //1.获取连接对象
            connection = configuration.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //2.获取SQl语句
        Map<String, Mapper> mappers = configuration.getMappers();
        Mapper mapper = mappers.get(Key);
        String sql = mapper.getSql();
        String resultType = mapper.getResultType();
        try {
            //3.预编译SQL语句
            pstm = connection.prepareStatement(sql);
            //4.执行Sql语句
            resultSet = pstm.executeQuery();
            //5.将结果集封装到JavaBean里面----->反射技术
            List<T> list = Converser.converList(resultSet, Class.forName(resultType));
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (pstm != null) {
                    pstm.close();
                }
                if (connection != null) {
                    connection.close();
                }
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public Object query(String Key,Object parameter){
        Connection connection = null;
        PreparedStatement pstm = null;
        ResultSet resultSet = null;
        try {
            //1.获取连接对象
            connection = configuration.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //2.获取SQl语句
        Map<String, Mapper> mappers = configuration.getMappers();
        Mapper mapper = mappers.get(Key);
        String sql = mapper.getSql();
        String resultType = mapper.getResultType();
        try {

            //3.预编译SQL语句
          pstm = connection.prepareStatement(sql);

          // pstm = connection.prepareStatement(SQLTokenParser.parse(sql));

            //设置参数
            if (parameter instanceof Integer) {
                pstm.setInt(1, (Integer) parameter);
            } else {
            }


            //4.执行Sql语句
            resultSet = pstm.executeQuery();
            //5.将结果集封装到JavaBean里面----->反射技术
            return Converser.converSelectid(resultSet, Class.forName(resultType));

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (pstm != null) {
                    pstm.close();
                }
                if (connection != null) {
                    connection.close();
                }
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public int delete(String Key,Object parameter){
        Connection connection = null;
        PreparedStatement pstm = null;
        ResultSet resultSet = null;
        try {
            //1.获取连接对象
            connection = configuration.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //2.获取SQl语句
        Map<String, Mapper> mappers = configuration.getMappers();
        Mapper mapper = mappers.get(Key);
        String sql = mapper.getSql();
       // String resultType = mapper.getResultType();
        try {

            //3.预编译SQL语句
            pstm = connection.prepareStatement(sql);

            // pstm = connection.prepareStatement(SQLTokenParser.parse(sql));

            //设置参数
            if (parameter instanceof Integer) {
                pstm.setInt(1, (Integer) parameter);
            } else {
            }


            //4.执行Sql语句
            int res= pstm.executeUpdate();
            //boolean res = pstm.execute();
            //5.将结果集封装到JavaBean里面----->反射技术
           // return Converser.converDeleteid(resultSet, Class.forName(resultType));
             return res;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (pstm != null) {
                    pstm.close();
                }
                if (connection != null) {
                    connection.close();
                }
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }



    @Override
    public int update(String Key,String parameter){

        Connection connection = null;
        PreparedStatement pstm = null;
        ResultSet resultSet = null;
        try {
            //1.获取连接对象
            connection = configuration.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //2.获取SQl语句
        Map<String, Mapper> mappers = configuration.getMappers();
        Mapper mapper = mappers.get(Key);
        String sql = mapper.getSql();
        // String resultType = mapper.getResultType();
        try {
            String[] str = parameter.split(" ");
            int id = Integer.valueOf(str[0]);

            String username=str[1];
            String birthday=str[2];
            String sex=str[3];
            String address=str[4];


            //3.预编译SQL语句
            pstm = connection.prepareStatement(sql);

            // pstm = connection.prepareStatement(SQLTokenParser.parse(sql));

            //设置参数

                pstm.setInt(5, id);
                pstm.setString(1,username);
                pstm.setString(2,birthday);
                pstm.setString(3,sex);
                pstm.setString(4,address);

            //4.执行Sql语句
            int res= pstm.executeUpdate();

            return res;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (pstm != null) {
                    pstm.close();
                }
                if (connection != null) {
                    connection.close();
                }
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    public void close() {


    }
}
